package com.tsd.system.service.impl;

import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.system.service.SysSettingService;
import com.tsd.system.service.WxSettingService;
import com.tsd.system.vo.MiniappSetting;
import com.tsd.system.vo.MpSetting;
import com.tsd.system.vo.OpenSetting;
import com.tsd.system.vo.WxPaySetting;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName: WxSettingServiceImpl
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/7/6 16:08
 * @Version: 1.0
 */
@Service
public class WxSettingServiceImpl implements WxSettingService {

    @Resource
    private SysSettingService sysSettingService;

    private static final Map<String, OpenSetting> WX_OPEN_SETTING_MAP;
    private static final Map<String, MpSetting> WX_MP_SETTING_MAP;
    private static final Map<String, MiniappSetting> WX_MINIAPP_SETTING_MAP;
    private static final Map<String, WxPaySetting> WX_PAY_SETTING_MAP;

    static {
        WX_OPEN_SETTING_MAP = new ConcurrentHashMap<>();
        WX_MP_SETTING_MAP = new ConcurrentHashMap<>();
        WX_MINIAPP_SETTING_MAP = new ConcurrentHashMap<>();
        WX_PAY_SETTING_MAP = new ConcurrentHashMap<>();
    }

    /**
     * 获取微信开放平台配置
     *
     * @return
     * @throws Exception
     */
    @Override
    public OpenSetting getWxOpenSetting() throws Exception {
        String account = DataSourceContextHolder.getDataSourceType();
        OpenSetting setting = WX_OPEN_SETTING_MAP.get(account);
        if (setting == null) {
            setting = sysSettingService.queryValidSettingByCode(OpenSetting.SETTING_CODE_WX_OPEN, OpenSetting.class);
            if (setting != null) {
                WX_OPEN_SETTING_MAP.put(account, setting);
            }
        }
        return setting;
    }

    /**
     * 清除微信开放平台配置的缓存
     */
    @Override
    public void clearWxOpenCache() {
        String account = DataSourceContextHolder.getDataSourceType();
        WX_OPEN_SETTING_MAP.remove(account);
    }

    /**
     * 获取微信公众号配置
     *
     * @return
     * @throws Exception
     */
    @Override
    public MpSetting getWxMpSetting() throws Exception {
        String account = DataSourceContextHolder.getDataSourceType();
        MpSetting setting = WX_MP_SETTING_MAP.get(account);
        if (setting == null) {
            setting = sysSettingService.queryValidSettingByCode(MpSetting.SETTING_CODE_WX_MP, MpSetting.class);
            if (setting != null) {
                WX_MP_SETTING_MAP.put(account, setting);
            }
        }
        return setting;
    }

    /**
     * 清除微信公众号配置的缓存
     */
    @Override
    public void clearWxMpCache() {
        String account = DataSourceContextHolder.getDataSourceType();
        WX_MP_SETTING_MAP.remove(account);
    }

    /**
     * 获取微信小程序配置
     *
     * @return
     * @throws Exception
     */
    @Override
    public MiniappSetting getWxMiniappSetting() throws Exception {
        String account = DataSourceContextHolder.getDataSourceType();
        MiniappSetting setting = WX_MINIAPP_SETTING_MAP.get(account);
        if (setting == null) {
            setting = sysSettingService.queryValidSettingByCode(MiniappSetting.SETTING_CODE_WX_MINIAPP, MiniappSetting.class);
            if (setting != null) {
                WX_MINIAPP_SETTING_MAP.put(account, setting);
            }
        }
        return setting;
    }

    /**
     * 清除微信小程序配置的缓存
     */
    @Override
    public void clearWxMiniappCache() {
        String account = DataSourceContextHolder.getDataSourceType();
        WX_MINIAPP_SETTING_MAP.remove(account);
    }

    @Override
    public WxPaySetting getWxPaySetting() throws Exception {
        String account = DataSourceContextHolder.getDataSourceType();
        WxPaySetting setting = WX_PAY_SETTING_MAP.get(account);
        if (setting == null) {
            setting = sysSettingService.queryValidSettingByCode(WxPaySetting.SETTING_CODE_WX_PAY, WxPaySetting.class);
            if (setting != null) {
                WX_PAY_SETTING_MAP.put(account, setting);
            }
        }
        return setting;
    }

    @Override
    public void clearWxPaySettingCache() {
        String account = DataSourceContextHolder.getDataSourceType();
        WX_PAY_SETTING_MAP.remove(account);
    }
}
